package com.atguigu.crowd.funding.handler;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.crowd.funding.entity.Auth;
import com.atguigu.crowd.funding.entity.ResultEntity;
import com.atguigu.crowd.funding.service.api.AuthService;

@RestController
public class AuthHandler {

	@Autowired
	private AuthService authService;
	
	/**
	 * 获取全部权限数据
	 * @return
	 */
	@RequestMapping("/auth/get/all")
	public ResultEntity<List<Auth>> getAllAuth(){
		List<Auth> list=authService.getAllAuth();
		return ResultEntity.successWithoutData(list);
	}
	
	/**
	 * 获取角色分配的权限数据
	 * 查询中中间表
	 */
	@RequestMapping("/auth/get/roleauth")
	public ResultEntity<List<Auth>> getAuthByRole(
			@RequestParam("roleid") Integer roleid){
		
		List<Auth> list=authService.getAuthByRole(roleid);
		
		return ResultEntity.successWithoutData(list);
	}
	
	/**
	 * 给用户分配权限
	 *先删除原有的，再保存新的
	 * 保存中间（roleid,authid）
	 */
	@RequestMapping("/auth/assignauth")
	public ResultEntity<String> AssignAuth(
			/*@RequestParam("roleid") Integer roleid,*/
			@RequestBody Map<String,List<Integer>> authmap){
		
		try {
			List<Integer> roleid=authmap.get("roleid");
			List<Integer> authids=authmap.get("authIdList");
			System.out.println(roleid.get(0));
			System.out.println(authids.size());
			authService.updateRoleAuth(roleid.get(0),authids);
			return ResultEntity.successWithoutData();
		} catch (Exception e) {
			// TODO: handle exception
			return ResultEntity.failed(null, e.getMessage());
		}
		
	}
}

